package sk.marek.barak.server;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;

public class TokenGAEDAO implements TokenDAO {

	@Override
	public List<String> getTokens() {
		List<String> tokens = null;
		MemcacheService cache = MemcacheServiceFactory.getMemcacheService();
		User curentUser = UserServiceFactory.getUserService().getCurrentUser();
		Key key = KeyFactory.createKey("Token",curentUser.getUserId());
		tokens = (List<String>)cache.get(key);
		if(tokens!=null)
		cache.delete(tokens);
		return tokens;
	}

	@Override
	public void sendToken(String user) {
		MemcacheService cache = MemcacheServiceFactory.getMemcacheService();
		Key key = KeyFactory.createKey("Token", user);
		List<String> tokens = (List<String>)cache.get(key);
		if(tokens==null)
			tokens = new ArrayList<String>();
		String id = UserServiceFactory.getUserService().getCurrentUser().getUserId();
		if(!tokens.contains(id))
		tokens.add(id);
		cache.put(key, tokens);;
		}


}
